<?php

/**
 * 默认接口服务类
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Api_Event extends PhalApi_Api
{

    public function getRules()
    {
        return array(
            'createEvent' => array(
                'illness' => array('name' => 'Illness', 'require' => true,),
                'name' => array('name' => 'Name'),
                'call_phone' => array('name' => 'CallPhone'),
                'patient_id' => array('name' => 'PatientId'),
                'provice' => array('name' => 'Provice'),
                'city' => array('name' => 'City'),
                'district' => array('name' => "District"),
                'detail' => array('name' => "Detail"),
            ),
            'listEvents' => array(),
            'setDoctorToEvent' => array(
                'event_id' => array('name' => 'EventId', 'require' => true),
                'doctor_id' => array('name' => 'DoctorId', 'require' => true),
            ),
            'getEventsOfDoctor' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true),
            ),
            'setComment' => array(
                'comment_id' => array('name' => 'CommentId'),
                'event_id' => array('name' => 'EventId', 'require' => true),
                'doctor_id' => array('name' => 'DoctorId', 'require' => true),
                'content' => array('name' => 'Content'),
            ),
            'setCommentImage' => array(
                'comment_id' => array('name' => 'CommentId', 'require' => true),
                'image' => array(
                    'name' => 'image',
                    'requier' => true,
                    'type' => 'file',
                    'min' => 0,
                    'max' => 1024 * 1024,
                    'range' => array('image/jpg', 'image/jpeg', 'image/png'),
                    'ext' => array('jpg', 'jpeg', 'png'))
            ),
            'setTest' => array(
                'name' => array('name' => 'Name'),
                'call_phone' => array('name' => 'CallPhone'),
                'illness' => array('name' => 'Illness'),
                'patient_id' => array('name' => 'PatientId'),
                'provice' => array('name' => 'Provice'),
                'district' => array('name' => "District"),
                'detail' => array('name' => "Detail")
            ),
            'getEventInfo' => array(
                'event_id' => array('name' => 'EventId', 'require' => true),
            ),
            'listDispatch' => array(
                'event_id' => array('name' => 'EventId'),
                'doctor_id' => array('name' => 'DoctorId')
            ),
            'getDispatch' => array(
                'dispatch_id' => array('name' => 'DispatchId', 'require' => true)
            ),
            'listDoctorOfEvent' => array(
                'event_id' => array('name' => 'EventId', 'require' => true),
            )
        );
    }

    public function setTest()
    {
        $in = array('name' => $this->name, 'call_phone' => $this->call_phone, 'provice' => $this->provice);
        return $in;
    }

    /**
     * 新建事件
     * @desc 新建事件
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function createEvent()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Event();
        $info = $domain->createEvent(
            $this->name,
            $this->illness,
            $this->patient_id,
            $this->call_phone,
            $this->provice,
            $this->city,
            $this->district,
            $this->detail
        );

        if (empty($info)) {
            DI()->logger->debug('', $this->illness);

            $rs['code'] = 1;
            $rs['msg'] = T('user not exists');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of event createEvent', $rs);

        return $rs;

    }

    /**
     * 列出所有事件
     * @desc 列出所有事件
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return    string    info id    事件id
     * @return    string    info Illness    病症
     * @return    string    info Illness_info    详细病症
     * @return    string    info CreateDate    创建时间
     * @return    string    info Doctor_id    医生id
     * @return    string    info Doctor_Name    医生姓名
     * @return    string    info Location_id    定位信息id
     * @return    string    info Address    地址
     * @return    string    info Patient_id    病人id
     * @return    string    info Patient_Name    病人姓名
     * @return string msg 提示信息
     */
    public function listEvents()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));
        $domain = new Domain_Event();
        $info = $domain->listEvents();

        if (empty($info)) {
            DI()->logger->debug('', '');

            $rs['code'] = 1;
            $rs['msg'] = T('没有事件');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of login', $rs);

        return $rs;

    }

    /**
     * 分配医生给事件
     * @desc 分配医生给事件
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function setDoctorToEvent()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Event();
        $info = $domain->setDoctorToEvent($this->event_id, $this->doctor_id);

        if (empty($info)) {
            DI()->logger->debug('info of setDoctorToEvent ', '');

            $rs['code'] = 1;
            $rs['msg'] = T('不需要分配医生');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of setDoctorToEvent', $rs);

        return $rs;

    }

    /**
     * 获取医生参加的事件
     * @desc 获取医生参加的事件
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function getEventsOfDoctor()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Event();
        $info = $domain->getEventsOfDoctor($this->doctor_id);

        if (empty($info)) {
            DI()->logger->debug('getEventsOfDoctor()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('获取医生参加的事件失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of getEventsOfDoctor()', $rs);

        return $rs;

    }

    /**
     * 上传评论
     * @desc 用户上传评论，有图片
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function setComment()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Event();
        //$_FILES["image"]["size"]
        $info = $domain->setComment(
            $this->comment_id,
            $this->event_id,
            $this->doctor_id,
            $this->content
        );

        if (empty($info)) {
            DI()->logger->debug('setComment()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('用户上传评论，有图片失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of setComment', $rs);

        return $rs;

    }

    /**
     * 添加评论图片
     * @desc 给评论添加图片
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function setCommentImage()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Event();
        $info = $domain->setCommentImage($this->comment_id, $this->image);

        if (empty($info)) {
            DI()->logger->debug('setCommentImage()', '');

            $rs['code'] = 1;
            $rs['msg'] = T(' 给评论添加图片失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of setCommentImage()', $rs);

        return $rs;

    }

    /**
     * 获取事件信息
     * @desc 获取单个事件信息
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function getEventInfo()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $domain = new Domain_Event();
        $info = $domain->getEventInfo($this->event_id);

        if (empty($info)) {
            DI()->logger->debug('getEventInfo()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('获取单个事件信息失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of getEventInfo()', $rs);

        return $rs;

    }

    /**
     * 获取事件相关的医生
     * @desc 获取事件相关的医生
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function listDoctorOfEvent()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Work();
        $info = $domain->listDoctorOfEvent($this->event_id);

        if (empty($info)) {
            DI()->logger->debug('listDoctorOfEvent()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('没有事件相关的医生');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of listDoctorOfEvent()', $rs);

        return $rs;

    }

}